package com.wangcanxuan.www.service.Impl;

import com.wangcanxuan.www.dao.mapper.*;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.*;
import com.wangcanxuan.www.exception.ServiceException;
import com.wangcanxuan.www.proxy.DaoProxy;
import com.wangcanxuan.www.service.UserService;

import com.wangcanxuan.www.util.SecurityUtil;
import com.wangcanxuan.www.util.ValidateUtil;

import java.time.LocalDateTime;

import java.util.Random;

import static com.wangcanxuan.www.entity.dto.ResultConstants.SUCCESS_REQUEST_CODE;
import static com.wangcanxuan.www.service.constant.ServiceConstants.*;

/**
 * 用户业务实现类
 *
 * @author HHLJ
 * @date 2022/03/26
 */
public class UserServiceImpl implements UserService {

    private final UserMapper USER_MAPPER = DaoProxy.getMapper(UserMapper.class);
    private final RoleMapper ROLE_MAPPER = DaoProxy.getMapper(RoleMapper.class);
    private final TribeMapper TRIBE_MAPPER = DaoProxy.getMapper(TribeMapper.class);


    @Override
    public ResultInfo<User> login(String userName, String password) {
        if (ValidateUtil.strIsNullOrEmpty(userName)) {
            return ResultInfo.operateRejectWithMessage("用户名不能为空！");
        }
        if (ValidateUtil.strIsNullOrEmpty(password)) {
            return ResultInfo.operateRejectWithMessage("密码不能为空！");
        }

        if (ValidateUtil.isNotPassword(password)) {
            return ResultInfo.operateRejectWithMessage("密码格式错误！");
        }

        // 设置要查询的用户的用户名和密码
        User selectUser = new User();
        selectUser.setUserName(userName);
        selectUser.setPassword(SecurityUtil.getSha1Encrypt(password));

        // 在数据库中查询是否有该用户
        User user = USER_MAPPER.selectOneByFields(selectUser);

        if (user == null) {
            return ResultInfo.operateRejectWithMessage("用户名或密码错误！");
        }

        return new ResultInfo<>(SUCCESS_REQUEST_CODE.getCode(), user);
    }

    @Override
    public ResultInfo<String> register(User insertUser) {
        // 设置设置要查询的用户的用户名
        User selectUserNameUser = new User();
        selectUserNameUser.setUserName(insertUser.getUserName());

        // 在数据库中查询是否存在拥有该用户名的用户
        User returnSelectUser = USER_MAPPER.selectOneByFields(selectUserNameUser);
        if (returnSelectUser != null) {
            return ResultInfo.operateRejectWithMessage("该用户名已存在！");
        }
        // 设置设置要查询的用户的昵称
        User selectNickNameUser = new User();
        selectNickNameUser.setNickName(insertUser.getNickName());
        // 在数据库中查询是否存在拥有该昵称的用户
        returnSelectUser = USER_MAPPER.selectOneByFields(selectNickNameUser);
        if (returnSelectUser != null) {
            return ResultInfo.operateRejectWithMessage("该昵称已存在！");
        }
        // 为用户的密码加密
        insertUser.setPassword(SecurityUtil.getSha1Encrypt(insertUser.getPassword()));
        // 自动生成uuid主键
        insertUser.setId(SecurityUtil.uuidCreateId());
        int insert = USER_MAPPER.insert(insertUser);
        if (insert != 1) {
            throw new ServiceException("注册功能异常！");
        }

        return new ResultInfo<>(SUCCESS_REQUEST_CODE.getCode());
    }

    @Override
    public ResultInfo<Role> getUserRole(String userId) {
        // 通过id来查询当前用户
        User selectUser = new User(userId);
        selectUser = USER_MAPPER.selectById(selectUser);
        // 通过用户中的roleId来获得当前职位
        Role role = new Role();
        role.setId(selectUser.getRoleId());
        role = ROLE_MAPPER.selectById(role);

        return new ResultInfo<>(SUCCESS_REQUEST_CODE.getCode(), role);
    }

    @Override
    public ResultInfo<User> signIn(String id) {
        // 查询当前用户
        User selectUser = new User(id);
        selectUser = USER_MAPPER.selectById(selectUser);
        // 判断该用户是否有签到过，如果没有签到过就设置一个签到日期
        if (selectUser.getSignDate() == null) {
            selectUser.setSignDate(SecurityUtil.yesterdayDateTime());
            int update = USER_MAPPER.update(selectUser);
            if (update != 1) {
                throw new ServiceException("签到功能异常！");
            }
        }

        // 检测签到时长是否超过一天
        if (SecurityUtil.isLessThanOneDay(selectUser.getSignDate())) {
            return ResultInfo.operateRejectWithMessage("请明天再来拉伸吧！");
        }

        // 随机增加的金币
        Random random = new Random();
        // 50-100金币
        int addCoins = random.nextInt(51) + 50;
        int newCoins = selectUser.getCoins() + addCoins;
        // 更新用户的当前金币和签到时间
        User updateUser = new User();
        updateUser.setId(id);
        updateUser.setCoins(newCoins);
        // 更改签到日期为当前时间
        updateUser.setSignDate(LocalDateTime.now());
        int update = USER_MAPPER.update(updateUser);
        if (update != 1) {
            throw new ServiceException("签到功能异常！");
        }

        selectUser = USER_MAPPER.selectOneByFields(updateUser);
        return new ResultInfo<>(SUCCESS_REQUEST_CODE.getCode(), selectUser);
    }

    @Override
    public ResultInfo<User> refresh(String id) {
        // 获取当前用户
        User user = USER_MAPPER.selectById(new User(id));
        return new ResultInfo<>(SUCCESS_REQUEST_CODE.getCode(), user);
    }

    @Override
    public ResultInfo<String> updateUserMessage(User user) {
        // 获取当前用户
        User selectUser = new User();
        // 设置昵称，查询昵称是否重复
        selectUser.setNickName(user.getNickName());
        selectUser = USER_MAPPER.selectOneByFields(selectUser);

        if (selectUser != null) {
            return ResultInfo.operateRejectWithMessage("该昵称已存在！");
        }

        // 为新密码加密
        user.setPassword(SecurityUtil.getSha1Encrypt(user.getPassword()));
        int update = USER_MAPPER.update(user);
        if (update != 1) {
            throw new ServiceException("更新信息功能异常！");
        }

        // 判断是否为当前首领
        if (user.getRoleId() == LEADER_ID) {
            // 查询当前所在部落
            Tribe selectTribe = new Tribe();
            selectTribe.setId(user.getTribeId());
            Tribe tribe = TRIBE_MAPPER.selectById(selectTribe);
            if (tribe != null) {
                // 更改部落当前首领信息
                tribe.setCurrentLeader(user.getNickName());
                int update1 = TRIBE_MAPPER.update(tribe);
                if (update1 != 1) {
                    throw new ServiceException("更新信息功能异常！");
                }
            }
        }
        return new ResultInfo<>(SUCCESS_REQUEST_CODE.getCode());
    }


    @Override
    public ResultInfo<String> findPassword(User user) {
        // 通过用户名查询当前用户的信息
        User selectUser = new User();
        selectUser.setUserName(user.getUserName());
        selectUser = USER_MAPPER.selectOneByFields(selectUser);
        //比较手机号
        if (!selectUser.getPhone().equals(user.getPhone())) {
            return ResultInfo.operateRejectWithMessage("手机号不一致！");
        }

        //比较邮箱地址
        if (!selectUser.getEmailAddress().equals(user.getEmailAddress())) {
            return ResultInfo.operateRejectWithMessage("邮箱地址不一致！");
        }

        //为用户的密码加密
        selectUser.setPassword(SecurityUtil.getSha1Encrypt(user.getPassword()));
        int update = USER_MAPPER.update(selectUser);
        if (update != 1) {
            throw new ServiceException("找回密码功能异常！");
        }
        return new ResultInfo<>(SUCCESS_REQUEST_CODE.getCode());
    }
}
